{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec58d47e",
   "metadata": {},
   "source": [
    "| [01_base/03_元组类型_tuple.ipynb](https://github.com/shibing624/python-tutorial/blob/master/01_base/03_元组类型_tuple.ipynb)  | Python元组  |[Open In Colab](https://colab.research.google.com/github/shibing624/python-tutorial/blob/master/01_base/03_元组类型_tuple.ipynb) |\n",
    "\n",
    "\n",
    "# tuple\n",
    "\n",
    "与列表相似，元组tuple也是个有序序列，但是元组是不可变的，用()生成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "7a258ee9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 11, 12, 13, 14)\n",
      "10\n",
      "(11, 12)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(11, 12)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (10, 11, 12, 13, 14)\n",
    "print(a)\n",
    "\n",
    "# 可以索引，切片：\n",
    "c = a[0]\n",
    "print(c)\n",
    "\n",
    "c = a[1:3]\n",
    "print(c)  # (11, 12)\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c19d19e8",
   "metadata": {},
   "source": [
    "\n",
    "单个元素的元组生成\n",
    "\n",
    "采用下列方式定义只有一个元素的元组：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "769295cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10,)\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "a = (10,)\n",
    "print(a)\n",
    "print(type(a))  # <type 'tuple'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a5495d37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = tuple(a)\n",
    "print(b)  # (1, 2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8925b5e3",
   "metadata": {},
   "source": [
    "由于元组是不可变的，所以只能有一些不可变的方法，\n",
    "\n",
    "例如计算元素个数 count 和元素位置 index ，用法与列表一样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4e531c58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "c = a.count(1)\n",
    "print(c)  # 1\n",
    "\n",
    "c = a.index(3)\n",
    "print(c)  # 索引位置为：2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcabab23",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}